package com.test.config.page;


import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;

/**
 * 通用分页数据封装返回
 *
 * Created by lijinze
 * on
 */
@Data
public class PageResp<T> {

    private Integer pageNum;
    private Integer pageSize;
    private Integer totalPage;
    private Long totalCount;
    private List<T> list;

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> PageResp<T> restPage(List<T> list) {
        PageResp<T> result = new PageResp<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    public static <T> PageResp<T> restPage() {
        PageResp<T> result = new PageResp<T>();
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
    public static <T> PageResp<T> restPage(PageInfo<T> pageInfo) {
        PageResp<T> result = new PageResp<T>();
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
    public static <T> PageResp<T> restPage(PageResp pageResp, List<T> list) {
        PageResp<T> result = new PageResp<T>();
        result.setTotalPage(pageResp.getTotalPage());
        result.setPageNum(pageResp.getPageNum());
        result.setPageSize(pageResp.getPageSize());
        result.setTotalCount(pageResp.getTotalCount());
        result.setList(list);
        return result;
    }


}
